package com.xzl.manger.server;

import com.xzl.manger.dao.IOrderDao;
import com.xzl.manger.model.Goods;
import com.xzl.manger.model.Order;
import com.xzl.manger.model.OrderGood;
import com.xzl.shop.model.User;
import com.xzl.shop.server.UserServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class OrderServer {
    @Autowired
    private IOrderDao orderDao;
    @Autowired
    private UserServer userServer;
    public List<Order> selectOrder(Order order){
        return orderDao.selectOrders(order);
    }
    public List<Order> selectOrderOk(Order order){
        return orderDao.selectOrdersOk(order);
    }
    public List<Order> selectOrderNo(Order order){
        return orderDao.selectOrdersNo(order);
    }
    public Integer insertOrder(Order order){
        if(order!=null) return orderDao.insertOrder(order);
        else return 0;
    }
    public List<OrderGood> selectOrderGood(OrderGood og){
        return orderDao.selectOrderGood(og);
    }
    public Integer insertOrderGood(OrderGood og){
        if(og!=null) return orderDao.insertOrderGood(og);
        else return 0;
    }
    public Integer receiveOrder(Order order){
        if(order == null || order.getOrderStatus() == null || order.getOrderStatus() != 3 ) return 0;
        order.setPreStatus(order.getOrderStatus());
        order.setOrderStatus(4);
        return orderDao.updateOrderStatus(order);
    }
    public void receiveOrders(String ids){
        String[] idsz = ids.split(",");
        for (String id : idsz){
            List<Order> list = selectOrder(new Order(id));
            receiveOrder(list.get(0));
        }
    }
    public Integer refundOrder(Order order){
        if(order == null || order.getOrderStatus() == null || order.getOrderStatus() < 2 || order.getOrderStatus() > 3) return 0;
        order.setPreStatus(order.getOrderStatus());
        order.setOrderStatus(5);
        return orderDao.updateOrderStatus(order);
    }
    public void refundOrders(String ids){
        String[] idsz = ids.split(",");
        for (String id : idsz){
            List<Order> list = selectOrderOk(new Order(id));
            refundOrder(list.get(0));
        }
    }
    public void refundMoney(String ids){
        String[] idsz = ids.split(",");
        for (String id : idsz){
            Order order = orderDao.selectOrders(new Order(id)).get(0);
            Integer userId = order.getUserId();
            User user = new User();
            user.setId(userId);
            user = userServer.selectUser(user).get(0);
            user.setMoney(user.getMoney().add(order.getOrderMoney()));
            userServer.setMoney(user);
            orderDao.delete(id);
        }
    }
    public Integer expressOrder(Order order){
        if(order == null || order.getOrderStatus() == null || order.getOrderStatus() != 2) return 0;
        order.setPreStatus(order.getOrderStatus());
        order.setOrderStatus(3);
        System.err.println("成功更新订单+"+order.getOrderId());
        return orderDao.updateOrderStatus(order);
    }
    public void expressOrders(String ids){
        String[] idsz = ids.split(",");
        for (String id : idsz){
            List<Order> list = selectOrderOk(new Order(id));
            System.err.println("预更新id:"+list.get(0).getOrderId());
            expressOrder(list.get(0));
        }
    }
    public BigDecimal pay(String ids,Integer userId){
        if(userId == null || ids == null || ids.isEmpty()) return BigDecimal.ZERO;
        String[] idsz = ids.split(",");
        List<Order> list = new ArrayList<>();
        for (String id : idsz){
            Order order = new Order(id);
            order = orderDao.selectOrders(order).get(0);
            if(order.getOrderStatus()!=1) continue;
            list.add(order);
        }
        BigDecimal money = BigDecimal.ZERO;
        for (Order o : list){
            money = money.add(o.getOrderMoney());
        }
        User user = new User();
        user.setId(userId);
        user = userServer.selectUser(user).get(0);
        BigDecimal userMoney = user.getMoney();
        if(userMoney.compareTo(money)==-1) return BigDecimal.ZERO;
        userMoney = userMoney.subtract(money);
        user.setMoney(userMoney);
        userServer.setMoney(user);
        for (Order o : list){
            o.setPreStatus(o.getOrderStatus());
            o.setOrderStatus(2);
            orderDao.updateOrderStatus(o);
        }
        return money;
    }
    public Integer delete(String ids){
        Integer i = 0;
        String[] idsz = ids.split(",");
        List<Order> list = new ArrayList<>();
        for (String id : idsz){
            Order order = new Order(id);
            order = orderDao.selectOrders(order).get(0);
            if(order.getOrderStatus()==1) {
                orderDao.delete(id);
                i++;
            }
            if(order.getOrderStatus()==3||order.getOrderStatus()==2) {
                refundOrder(order);
                i++;
            }
        }
        return i;
    }
}
